package aufgabe1;

import java.util.ArrayList;
import java.util.LinkedList;

public class Graph {

	private static AdjacencyStructure _adjStructure;
	private static Graph g = new Graph();
	private static ArrayList<Edge> _edges;
	private static ArrayList<Vertice> _vertices;
	private static int[][] _matrix;
	private static LinkedList<Edge>[] liste;

	public Graph() {

	}

	public static void createGraph(String filename,
			AdjacencyStructure.Type adjStructure) {

		readFile(filename);

		switch (adjStructure) {
		case LIST:
			_adjStructure = new AdjacencyList(g);
			createList();

			break;
		case MATRIX:
			_adjStructure = new AdjacencyMatrix(g);
			createMatrix();
			break;
		}

	}

	public static void readFile(String filename) {

		GraphFileReader.ladeDatei(filename);
		_edges = GraphFileReader.getEdges();
		_vertices = GraphFileReader.getNodes();

	}

	/**
	 * used for adjMatrix and getNeighbours
	 */
	public static void createMatrix() {
		_matrix = new int[_vertices.size() + 1][_vertices.size() + 1];
		for (Edge egd : _edges) {
			int i = egd.getStart().getPos();

			int j = egd.getEnd().getPos();

			_matrix[i][j] = egd.getVlaue();
		}

	}

	public static void createList() {
		liste = new LinkedList[_edges.size()];
		int c = 0;
		for (int i = 1; i < liste.length; i++) {

			liste[i - 1] = new LinkedList<Edge>();

			for (Edge edges : _edges) {

				if (edges.getStart().getPos() == i) {

					liste[i - 1].add(_edges.get(c));

				}
				c++;
			}

			c = 0;

		}

	}

	public static ArrayList<Vertice> getNeighbours(int key) {

		return _adjStructure.getNeighbours(key);
	}

	public static boolean isConnected() {
		boolean flag = false;

		for (int i = 1; i <= _vertices.size(); i++) {
			if (getNeighbours(i).size() > 0) {
				flag = true;

			} else
				break;
		}
		return flag;
	}

	public LinkedList<Edge>[] getList() {
		return liste;
	}

	public int[][] getMatrix() {
		return _matrix;
	}

	public static void print() {
		_adjStructure.print();

	}

	public ArrayList<Edge> getEdges() {
		return _edges;
	}

	public Vertice getVertice(int value) {
		for (Vertice vertice : _vertices) {
			if (vertice.getPos() == value) {
				return vertice;
			}
		}
		return null;
	}

	public ArrayList<Vertice> getVertices() {
		return _vertices;
	}

	@Override
	public String toString() {
		System.out.println("" + getClass().getSimpleName()
				+ _adjStructure.getClass().getSimpleName());
		return super.toString();
	}

}
